var Teacher = {
	updateInit: function() {
		$("#update").click(Teacher.add);

		var id = getValue('id');
		if(id) {
			$("#title").text('修改讲师');
			$ajax({
				url: "/teacher/detail",
				data: {
					_id: id
				},
				success: function(res) {
					var teacher = res.result;
					$("#name").val(teacher.name);
					$("#desc").val(teacher.desc);
					$("#photoList").html('<img src="' + Core.IMG_VER + teacher.photo + '" style="width:100%;"><input id="photo" type="hidden" value="' + teacher.photo + '">');
				}
			});	
		}else { 
			$("#title").text('新建讲师');
		}


	},
	listInit: function(){ 
	   	Teacher.teacherJump(1);
	},
	teacherClick: function(pageclickednumber){
		var cont = GetLS("cont");
      	Teacher.teacherJump(pageclickednumber);
      	$("#listPager").pager({pagenumber: pageclickednumber, pagecount: cont, buttonClickCallback: Teacher.teacherClick });
	},
	teacherJump: function(pageno){
		var count = Res.sinCount;
      	var start = (pageno-1)*count;
		$ajax({
			url : "/teacher/list",
			data:{
				start: start,
				count: count
			},
			success: function(res){
				var list = res.result.results;
				var cont = parseInt(res.result.count/Res.sinCount);
                if((res.result.count)%Res.sinCount!=0){
                     cont++;
                }
                SetLS("cont",cont);
                if($("#listPager").children().length == 0)
                     $("#listPager").pager({ pagenumber: pageno, pagecount: cont, buttonClickCallback: Teacher.teacherClick });
				var html =  '<thead>' + 
                            '    <tr>' + 
                            '        <th class="span3">讲师名</th>' + 
                            '        <th class="span4">描述</th>' + 
                            '        <th class="span3">供应商</th>' + 
                            '        <th class="span2">操作</th>' + 
                            '    </tr>' + 
                            '</thead>' + 
                            '<tbody>';;
                var length = list && list.length || 0;
				for (var i = 0 ; i < length; i ++){
					html += "<tr>" + 
                            "    <td>" + list[i].name + "</td>" + 
                            "    <td>" + (list[i].desc?list[i].desc:"") + "</td>" + 
                            "    <td>" + 
                            "        <span>" + (list[i].user.name?list[i].user.name:"暂无") + "</span>" + 
                            "    </td>" + 
                            "    <td>" + 
                            // "        <div class='text-right'>" + 
                            "				<button data-id='" + list[i]._id + "' class='btn btn-success updateBtn'>修改</button>" + 
                            "				<button data-id='" + list[i]._id + "' class='btn btn-danger btn-sm removeBtn'>删除</button>" + 
                            // "        </div>" + 
                            "    </td>" + 
                            "</tr>";
				}
				html += "</tbody>";
				$("#teacherList").html(html);
				$(".updateBtn").click(function(){
					window.location.href = "teacherUpdate.html?id=" + $(this).data("id");
				});
				$(".removeBtn").click(function(){
					if (!confirm("确定要删除吗?"))
						return;
					var $this = $(this).parent().parent().parent();
					$ajax({
						url: "/teacher/remove",
						data: {
							_id: $(this).data("id")
						},
						success: function(res){
							$this.remove();
						}
					});
				});
			}
		});
	},
	add: function() {
		var name = $("#name").val();
		var desc = $("#desc").val();
		var photo = $("#photo").val();
		if (name == "" || desc == "") 
			return $alert("讲师名称和描述不能为空");
                 
		var id = getValue("id");
		var data = {};
		data.name = name;
		data.desc = desc;
		data.photo = photo;
		if(id)
			data._id = id;
		$ajax({
			url: "/teacher/upsert",
			data: data,
			success: function(res) {
				if(res.code)
					return alert(res.codemsg);
				window.location.href = "/teacherList.html";
			}
		});
	},
}

